package com.example.controller;

import com.example.common.Result;
import com.example.domain.Employee;
import com.example.service.EmployeeService;
import com.github.pagehelper.PageInfo;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

//查全部数据
    @GetMapping("/selectAll")
    public Result selectAll(Employee employee){
        List<Employee> list = employeeService.selectAll(employee);
        return Result.success("查询成功",list);
    }

//    @RequestBody传数组            @PathVariable适用场景：根据ID查询资源
// 查单个数据
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Long id){
        Employee employee = employeeService.selectByPrimaryKey(id);
        return Result.success("查询成功",employee);
    }

//分页查询数据   pageNum:当前页  pageSize：每页个数  -------  要先导入依赖
    @GetMapping("/selectPage")
    public Result selectPage( Employee employee,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize){
        PageInfo<Employee> pageInfo = employeeService.selectPage(employee,pageNum, pageSize);
        return Result.success("查询成功",pageInfo);
    }

// 新增数据
    @PostMapping("/add")
    public Result add(@RequestBody Employee employee){
        employeeService.add(employee);
        return Result.success("新增成功",null);
    }

//根据id  更新数据
    @PostMapping("/update")
    public Result update(@RequestBody Employee employee){
        employeeService.update(employee);
        return Result.success("更新成功",null);
    }

//根据id  删除数据
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        employeeService.deleteById(id);
        return Result.success("删除成功", null);
    }

//    批量删除数据
    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        employeeService.deleteBatch(ids);
        return Result.success("删除成功", null);
    }
}
